<script setup lang="ts">
import { User,Lock } from '@element-plus/icons-vue';
import { ref } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { useUserStore } from '@/stores/modules/userStore';
import { getTime } from '@/utils/time';
//收集表单数据
const form=ref()
const router=useRouter()
const loading=ref(false)
const userStore=useUserStore()
const route=useRoute()
const redirect:any=route.query.redirect
//定义表单数据
const loginForm=ref({
    username:'admin',
    password:'111111'
})

//自定义表单规则
const checkUsername=(_rule:any,value:any,callback:any)=>{
  if(value.length >=5){
    callback()
  }else{
    callback(new Error('请输入正确的账号格式'))
  }
}

const checkPssword=(_rule:any,value:any,callback:any)=>{
  if(value.length >= 6){
    callback()
  }else{
    callback(new Error('请输入正确的密码格式'))
  }
}
//定义表单规则
const rules={
    username:[
        {required:true,message:'账号不能为空',trigger:'blur'},
        {validator:checkUsername,trigger:'blur'}
    ],
    password:[
        {required:true,message:'密码不能为空',trigger:'blur'},
        {validator:checkPssword,trigger:'blur'}
    ]
}


//提交表单
const submitForm=async()=>{
    await form.value.validate()
    loading.value=true
    try {
    await userStore.login(loginForm.value);
    form.value.resetFields()
    loading.value=false
    router.push({path:redirect|| '/'});
    //登录成功提示信息
    ElNotification.success({
      message: '欢迎回来',
      title: `HI,${getTime()}好`,
    });
  } catch (error) {
    loading.value=false
    ElNotification.error({
      message:(error as Error).message,
    })
  }
}
</script>

<template>
    <div class="login_container">
        <el-row>
            <el-col :span="12" :xs="0"></el-col>
            <el-col :span="12" :xs="24">
                <el-form ref="form" :rules="rules" :model="loginForm" label-width="80px" class="login_form">
                    <h1>Hello</h1>
                    <h2>欢迎来到硅谷甄选</h2>
                        <el-form-item prop="username">
                            <el-input :prefix-icon="User" v-model="loginForm.username" placeholder="请输入用户名"></el-input>
                        </el-form-item>
                        <el-form-item prop="password">
                            <el-input  type="password" v-model="loginForm.password" :prefix-icon="Lock" show-password placeholder="请输入密码"></el-input>
                        </el-form-item>
                        <el-form-item>
                            <el-button :loading="loading"  type="primary" class="login_btn" @click="submitForm">登录</el-button>
                        </el-form-item>
                </el-form>
            </el-col>
        </el-row>
    </div>
</template>

<style scoped lang="scss">
.login_container {
  width: 100%;
  height: 100vh;
  background: url('@/assets/images/background.jpg') no-repeat;
  background-size: cover;

  .login_form {
    position: relative;
    width: 80%;
    top: 30vh;
    background: url("@/assets/images/login_form.png") no-repeat;
    background-size: cover;
    padding: 40px;

    h1 {
      color: white;
      font-size: 40px;
    }

    h2 {
      font-size: 20px;
      color: white;
      margin: 20px 0px;
    }

    .login_btn {
      width: 100%;
    }
  }
}

</style>